/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Shared;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author hanv
 */
public class GameMapObj {

    public static int MAX_USER_IN_MAP = 10;
    public static int MAX_SLOT = 6;
    public static int MAP_USER_DIFFERENCES = 3;
//    public static int MAP_TYPE_MULTI_PLAYER = 0;
//    public static int MAP_TYPE_ONE_PLAYER = 1;
    public static Logger log = LoggerFactory.getLogger(GameMapObj.class.getName());
    public int mapID = 0;
    public String name = null;
    public String password = null;
    public int type = 0;
    public int money = 0;
    public int noOfZones = 0;
    private List userList = null;
    private int[] slotID = null;
//    public int numOfUser = 0;       // so nguoi hien co trong phong chat
//    public int maxUser = 15;        // so nguoi toi da

    public GameMapObj(int mapID, String Name) {
        this.mapID = mapID;
        name = Name;
//        type = Type;
        userList = Collections.synchronizedList(new ArrayList());
        /*
         * Map Fishing: 20.000 to 29.999
         */
        if (mapID / 10000 == 2) {
            slotID = new int[MAX_SLOT];
        }
    }

    public boolean isFull() {
        return (userList.size() == MAX_USER_IN_MAP);
    }

    public boolean isEmpty() {
        return userList.isEmpty();
    }

    public boolean containUser(String userName) {
        return userList.contains(userName);
    }

    public void addUser(String userName) {
        userList.add(userName);
    }

    public void removeUser(String userName) {
        userList.remove(userName);
    }

    public int getNumberOfUsers() {
        return userList.size();
    }

    public String getUserAt(int index) {
        String userName = null;
        try {
            if ((index >= 0) && (index < userList.size())) {
                userName = (String) userList.get(index);
            }
        } catch (Exception e) {
        }
        return userName;
    }

    public GameMapObj clone(int mapID) {
        GameMapObj gm = new GameMapObj(mapID, this.name);
        gm.password = password;
        gm.type = type;
        gm.money = money;
        gm.noOfZones = noOfZones;
        return gm;
    }

    /*
     * Fishing Slot Management
     */
    public boolean checkSlotValid(int slotId, int userId) {
        if (slotID[slotId] == 0) {
            slotID[slotId] = userId;            
            return true;
        } else {
            return false;
        }
    }

    public void leaveslot(int userid) {
        for (int i = 0; i < slotID.length; i++) {
            if (slotID[i] == userid) {
                slotID[i] = 0;                
            }
        }
    }

    public int getSlotId(int userid) {
        for (int i = 0; i < slotID.length; i++) {
            if (slotID[i] == userid) {
                return i;
            }
        }
        return -1;
    }

    public byte[] getSlotsStatus() {        
        byte[] result = new byte[MAX_SLOT];
        for (int i = 0; i < slotID.length; i++) {            
            if (slotID[i] == 0) {
                result[i] = 0;
            } else {
                result[i] = 1;
            }
        }
        return result;
    }
}
